#include"SeqList.h"





void SLInit(SL* ps)
{
	assert(ps);
	ps->a = malloc(sizeof(SLDataType) * 10);
	if (ps -> a == NULL)
	{
		perror("SLInit->malloc failed");
		exit(-1);
	}
	ps->size = 0;
	ps->capaicty = 10;
}

void SLDestroy(SL* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capaicty = 0;
}

void CheckCapacity(SL* ps)
{
	assert(ps);

	if (ps->size == ps->capaicty)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capaicty * 2);
		if (tmp == NULL)
		{
			perror("CheckCapacity->realloc failed");
			exit(-1);
		}
		ps->a = tmp;
		ps->capaicty *= 2;
	}
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);

	CheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

void SLPrint(SL* ps)
{
	assert(ps);

	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SLPopBack(SL* ps)
{
	assert(ps);

	assert(ps->size > 0);
	ps->size--;
}

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);

	CheckCapacity(ps);
	int i = 0;
	for (i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}

void SLPopFront(SL* ps)
{
	assert(ps);

	assert(ps->size > 0);
	int i = 0;
	for (i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}


void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);

	CheckCapacity(ps);
	assert(pos >= 0 && pos <= ps->size);
	int i = 0;
	for (i = ps->size; i >pos; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);

	assert(pos >= 0 && pos < ps->size);
	int i = 0;
	for (i = pos; i < ps->size-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);

	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}







